home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 323_01 / expa.asm < prev    next >
Assembly Source File  |  1990-08-04  |  16KB  |  843 lines

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ; expa.asm - support routines for EXPLOD.C.  Compile with "masm /mx expa ;".
  3. ;
  4. ; NOTE:
  5. ;   Make sure one of DLC or TURBOC is defined below.  C compilers other 
  6. ;   than Datalight C and Turbo C will probably require changes to the 
  7. ;   segment naming.  This file has been tested with the Arrowsoft Assembler 
  8. ;   1.00d and MASM 5.0.
  9. ;
  10. ; (C) 1989 Dennis Lo
  11. ; You are free to use and distribute this source code, provided that the
  12. ; authors' names remain in the code, and that modified versions are clearly 
  13. ; distinguished from the original.
  14. ;
  15. ; 89/06/24 Dennis Lo        (V1.0) Initial release. 
  16. ; 89/07/03 Dennis Lo        (V1.1) Added ifdefs for linking with Turbo C.
  17. ; 89/07/26 Erik Liljencrantz    Added EGA support with autodetection
  18. ;                Restores previous CRT textmode on exit
  19. ; 89/08/22 Dennis Lo        (V1.2) Allow frame buffer to be in any segment
  20. ;                Optimized HGC/CGA frame draw loop
  21. ;                Restricted EGA colours to only the bright ones
  22. ;                 to get rid of the flickering effect.
  23. ; 89/09/03 Erik Liljencrantz    Added VGA support with autodetection
  24. ; 89/09/23 Dennis Lo        Added DOS memory allocation routines
  25. ;                Cosmetic changes.
  26. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  27.  
  28. ;------------------------------------------------------------------
  29. ;        *** IMPORTANT: Uncomment one of the following ***
  30. ;
  31. ; uncomment this for Datalight C *******
  32. ;DLC     equ     1
  33. ;
  34. ; uncomment this for Turbo C ********
  35. TURBOC    equ    1
  36. ;
  37. ;------------------------------------------------------------------
  38.  
  39.  
  40. ;;;;;;;;;;;;;;;;;;;;;;;;; SEGMENT NAMES ;;;;;;;;;;;;;;;;;;;;;;;;;
  41. ;segment names for Datalight C
  42. ifdef DLC
  43. pgroup        group    prog
  44. prog        segment byte public 'prog'
  45.         assume    cs:pgroup
  46. prog        ends
  47. dgroup        group    data
  48. data        segment word public 'data'
  49.         assume ds:dgroup
  50. data        ends
  51. endif
  52.  
  53. ;segment names for Turbo C
  54. ifdef TURBOC
  55.         name    t
  56. _TEXT        segment byte public 'CODE'
  57. DGROUP        group    _DATA,_BSS
  58.         assume    cs:_TEXT,ds:DGROUP,ss:DGROUP
  59. _TEXT        ends
  60. _DATA        segment word public 'DATA'
  61. _d@        label    byte
  62. _DATA        ends
  63. _BSS        segment word public 'BSS'
  64. _b@        label    byte
  65. _BSS        ends
  66. endif
  67.  
  68.  
  69. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; CONST ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  70. ;    HGC port addresses
  71. config        equ    03bfh
  72. index        equ    03b4h
  73. cntrl        equ    03b8h
  74. ;    HGC control codes
  75. scrn_on        equ    8
  76. grph        equ    2
  77. text        equ    20h
  78.  
  79. par        equ    4        ;stack offset of 1st C call parameter
  80. DEADPOINT    equ    32767        ;flag for dead point in frame table
  81.  
  82.  
  83. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;; DATA ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  84. ifdef DLC
  85. data        segment word public 'data'
  86. endif
  87. ifdef TURBOC
  88. _DATA        segment word public 'DATA'
  89. endif
  90.  
  91. Vidtype     db    ?        ;video card type
  92. Vidseg        dw    ?        ;video mem segment
  93. Vidsize     dw    ?        ;video mem size
  94. SaveMode    DB    ?        ;Save video text mode
  95. Color        db    ?        ;Current frame colour (for EGA)
  96.  
  97. ;    Table of bit values for plotting points
  98. ;masktable    db    128,64,32,16,8,4,2,1    ;1-pixel wide points
  99. masktable    db    192,96,48,24,12,6,3,1    ;2-pixel wide points
  100.  
  101. ;    HGC 6845 config tables 
  102. hgc_gtable    db    35h,2dh,2eh,07h
  103.         db    5bh,02h,57h,57h
  104.         db    02h,03h,00h,00h
  105. hgc_ttable    db    61h,50h,52h,0fh
  106.         db    19h,06h,19h,19h
  107.         db    02h,0dh,0bh,0ch
  108.  
  109. ;    Palette register table for EGA
  110. ;    Consists of colour values for registers 0-15, plus border colour
  111. palette        db    00h,07h,3ah,3bh,3ch,3eh,3fh,07h
  112.         db    3ah,3bh,3ch,3eh,07h,3ah,3bh,3ch,00h
  113.  
  114. ifdef DLC
  115. data        ends
  116. endif
  117. ifdef TURBOC
  118. _DATA        ends
  119. endif
  120.  
  121.  
  122. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;; CODE ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  123. ifdef DLC
  124. prog        segment byte public 'prog'
  125. endif
  126. ifdef TURBOC
  127. _TEXT        segment byte public 'CODE'
  128. endif
  129.  
  130.  
  131. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  132. ; Return the number of paragraphs of memory available.
  133. ; int num_para = DosMemLeft ();
  134. ;
  135.         public    _DosMemLeft
  136. _DosMemLeft    proc    near
  137.         push    bp
  138.         mov    bp,sp
  139.         push    es
  140.  
  141.         mov    bx,0f000h    ;Call DOS malloc with a request for
  142.         mov    ah,48h        ; 960K to make sure it fails
  143.         int    21h
  144.         jc    memleft_cont
  145.  
  146.         xor    ax,ax        ;Error: DOS malloc() succeeded!
  147.         jmp    memleft_end    ;Return 0
  148.  
  149. memleft_cont:
  150.         mov    ax,bx        ;Return mem size remaining
  151.  
  152. memleft_end:
  153.         pop    es
  154.         pop    bp
  155.         ret
  156. _DosMemLeft    endp
  157.  
  158.  
  159. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  160. ; Allocate a block of memory 
  161. ; int segment_addr = DosAlloc (size_in_paragraphs);
  162. ; Returns 0 if unsucessful.
  163. ;
  164.         public    _DosAlloc
  165. _DosAlloc    proc    near
  166.         push    bp
  167.         mov    bp,sp
  168.         push    es
  169.  
  170.         mov    bx,par[bp]    ;get # paragraphs requested
  171. ;        add    bx,15        ;# paragraphs = (# bytes + 15) / 16
  172. ;        shr    bx,1
  173. ;        shr    bx,1
  174. ;        shr    bx,1
  175. ;        shr    bx,1
  176.         mov    ah,48h        ;call DOS malloc
  177.         int    21h
  178.         jnc    alloc_end
  179.         xor    ax,ax        ;if failure then return 0
  180. alloc_end:
  181.         pop    es
  182.         pop    bp
  183.         ret
  184. _DosAlloc    endp
  185.  
  186.  
  187. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  188. ; Free a block of memory allocated by DosAlloc
  189. ; DosFree (int segment_addr);
  190. ; Returns 0 if successful, 1 if not.
  191. ;
  192.         public    _DosFree
  193. _DosFree    proc    near
  194.         push    bp
  195.         mov    bp,sp
  196.         push    es
  197.  
  198.         mov    ax,par[bp]    ;get segment address to free at
  199.         mov    es,ax
  200.         mov    ah,49h        ;call DOS free
  201.         int    21h
  202.  
  203.         jc    free_end    ;if error then return DOS err code
  204.         xor    ax,ax        ;else return 0
  205. free_end:
  206.         pop    es
  207.         pop    bp
  208.         ret
  209. _DosFree    endp
  210.  
  211.  
  212. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  213. ; Nonblocking keyboard read - returns ASCII char
  214. ; int key = ChkKey();
  215. ;
  216.         public    _ChkKey
  217. _ChkKey        proc    near
  218.         push    si
  219.         push    di
  220.  
  221.         mov    ah,1
  222.         int    16h
  223.         jz    nokey        ;if no key then return
  224.         mov    ax,0        ;else get the key out of the buffer
  225.         int    16h
  226.         mov    ah,0
  227.         jmp    chkret
  228. nokey:
  229.         mov    ax,0
  230. chkret:
  231.         pop    di
  232.         pop    si
  233.         ret
  234. _ChkKey        endp
  235.  
  236.  
  237. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  238. ; Guess video card type.  Returns 'h', 'c', 'e', or 'v'.
  239. ; int card = gr_card();
  240. ;
  241.         public    _gr_card
  242. _gr_card    proc     near
  243.         push    bp
  244.         push    si
  245.         push    di
  246.     
  247.         mov    ah,0Fh
  248.         int    10h
  249.         MOV    SaveMode,AL    ;Save textmode to restore later...
  250.         MOV    BL,AL
  251.         mov    ax,'h'        ; Mono, assume HGC
  252.         cmp    bl,07h        ;
  253.         je    card_done    ;
  254.         MOV    AX,1200h
  255.         MOV    BX,0FF10h
  256.         MOV    CX,00FFh
  257.         INT    10h
  258.         CMP    CL,12       ;CL<12
  259.         JAE    Card_CGA
  260.         CMP    BH,1       ;BH<=1
  261.         JA    Card_CGA
  262.         CMP    BL,3       ;BL<=3
  263.         JA    Card_CGA
  264. ;OK, either EGA or VGA
  265. ;Use INT 10/AH=1A00 to determine which one...
  266.         MOV    AX,1A00h
  267.         INT    10h
  268.         CMP    AL,1Ah        ;Returns AL=1Ah if function supported
  269.         JNE    Card_EGA
  270.         CMP    BL,08h        ;VGA with analog color display
  271.         JNE    Card_EGA
  272.         MOV    AX,'v'        ;Assume VGA
  273.         JMP SHORT Card_Done
  274. Card_EGA:
  275.         MOV    AX,'e'        ;Assume EGA
  276.         JMP SHORT Card_Done
  277. Card_CGA:
  278.         MOV    AX,'c'        ;Assume CGA
  279. Card_Done:
  280.         pop    di
  281.         pop    si
  282.         pop    bp
  283.         ret
  284. _gr_card endp
  285.  
  286.  
  287. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  288. ; Set video card type
  289. ; gr_setcard (char video_card);
  290. ;
  291.         public    _gr_setcard
  292. _gr_setcard    proc    near
  293.         push    bp
  294.         mov    bp,sp
  295.     
  296.         mov    ax,par[bp]    ;get video_card parameter
  297.         mov    Vidtype,al
  298.  
  299.     ;Set video mem segment
  300.         cmp    al,'h'        
  301.         jne    not_herc
  302.         mov    ax,0b000h    ;Hercules
  303.         mov    cx,8000h    ; 32K bytes
  304.         jmp    s_end
  305. not_herc:
  306.         cmp    al,'e'        
  307.         jne    not_ega
  308.         mov    ax,0A000h    ;EGA
  309.         mov    cx,6D60h    ; 28000 bytes (but in four planes)
  310.         jmp    s_end
  311. not_ega:
  312.         cmp    al,'v'
  313.         jne    not_vga
  314.         mov    ax,0A000h    ;VGA
  315.         mov    cx,9600h    ; 38400 bytes (but in four planes)
  316.         jmp    s_end
  317. not_vga:
  318.         mov    ax,0b800h    ;CGA
  319.         mov    cx,4000h    ; 16K bytes
  320. s_end:
  321.         mov    Vidseg,ax
  322.         mov    Vidsize,cx
  323.  
  324.         pop    bp
  325.         ret
  326. _gr_setcard endp
  327.  
  328.  
  329. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  330. ; Set video card for graphics mode
  331. ; gr_gmode();
  332. ;
  333.         public    _gr_gmode
  334. _gr_gmode    proc    near
  335.         push    bp
  336.         push    es
  337.         push    ds
  338.         push    si
  339.  
  340.         mov    al,Vidtype
  341.         cmp    al,'h'
  342.         jne    NotHerc
  343.         call    hgc_gmode      ;HGC
  344.         jmp    g_end
  345. NotHerc:
  346.         cmp    al,'e'
  347.         jne    NotEga
  348.         call    ega_gmode      ;EGA
  349.         jmp    g_end
  350. NotEga:
  351.         cmp    al,'v'
  352.         jne    NotVga
  353.         call    vga_gmode      ;VGA
  354.         jmp    g_end
  355. NotVga:
  356.         call    cga_gmode      ;CGA
  357. g_end:
  358.         pop    si
  359.         pop    ds
  360.         pop    es
  361.         pop    bp
  362.         ret
  363. _gr_gmode    endp
  364.  
  365.  
  366. ;Set HGC graphics mode, page 0
  367. hgc_gmode:
  368.         mov    dx,config
  369.         mov    al,3
  370.         out    dx,al
  371.  
  372.         mov    al,grph
  373.         lea    si,hgc_gtable
  374.         mov    bx,0
  375.         mov    cx,4000h
  376.         call    hgc_setmd
  377.         ret
  378.  
  379.  
  380. ;Set EGA graphics mode
  381. ega_gmode:
  382.         mov    a